﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Xiaov.SmartWeb.Infrastructure.ExtMethod;
using Xiaov.SmartWeb.Infrastructure.Common;
using Xiaov.SmartWeb.Model.ActiveRecord;
using AjaxPro;
using Castle.ActiveRecord;
using Xiaov.SmartWeb.Model;

namespace Xiaov.SmartWeb.Application.SysAdmin
{
    public partial class Admin_User : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(Admin_User));
            //PM_FilterByProperty(ModulePropoty.MODULEURL, Request.Url.AbsolutePath);  
            PM_Filter();
            
        }

        [AjaxPro.AjaxMethod]
        public string RolesString(object o)
        {
            return "";
        }

        [AjaxPro.AjaxMethod]
        public bool SaveUser(object userid,object username,object loginname,object password,object disable,object superadmin,object sortby,object roles,object dwbm)
        {
            try
            {
                Xiaov.SmartWeb.Model.ActiveRecord.S_PM_USER model = new S_PM_USER();
                if(!string.IsNullOrEmpty(userid.ToString()))
                model = Model.ActiveRecord.S_PM_USER.Find(userid);                
                if (model==null||string.IsNullOrEmpty(model.USERID))
                {
                    //如果是新增 用户密码要加密 创建一个USERID
                    model.USERID = Guid.NewGuid().ToString();
                    model.PASSWORD = password.ToString().Encrypt();
                }
                else
                {
                    if (!string.IsNullOrEmpty(password.ToString()))
                    {
                        model.PASSWORD = password.ToString().Encrypt();
                    }
                }
                model.USERNAME = username.ToString();
                model.LOGINNAME = loginname.ToString();
                if (disable == null)
                    disable = "false";
                model.DISABLED = (disable.ToString() == "false" ? "0" : "1");
                if (superadmin == null)
                    superadmin = "false";
                model.SUPERADMIN = (superadmin.ToString() == "false" ? "0" : "1");
                model.USERSORT = sortby.ToInt32();
                model.USERDEPT = dwbm.ToRequestString();
                //保存用户
                model.Save();
                model.SaveRoles(roles);
                model.RefreshCache();                
                return true;
            }
            catch
            {
                return false;
            }
        }

        [AjaxPro.AjaxMethod]
        public bool DeleteUser(object userId)
        {
            try
            {
                Xiaov.SmartWeb.Model.ActiveRecord.S_PM_USER user = Model.ActiveRecord.S_PM_USER.Find(userId);
                S_PM_USERROLES.DeleteAll("USERID='" + userId + "'");                
                user.Delete();
                user.RefreshCache();
                return true;
            }
            catch
            {
                return false;
            }
            
        }

        [AjaxPro.AjaxMethod]
        public string GetRolesDescriptByUserId(object userId)
        {
            S_PM_USER user = new S_PM_USER();            
            return user.GetRolesDescriptByUserId(userId.ToString());
        }

        [AjaxPro.AjaxMethod]
        public string GetRolesIdByUserId(object userId)
        {
            S_PM_USER user = new S_PM_USER();
            return user.GetRolesIdByUserId(userId.ToString());
        }
    }
}